﻿/*
    File name:          Brick.cs    
    File type:          Class
    Author:             Jakob Sulby 09136
    Creation date:      29-03-2011
    Last modified:      29-03-2011
    Last modified by:   Jakob Sulby 09136
    Description:        This class defines the bricks variables. Further more it automaticly validates if all the variables 
                        is set and if so uploades the brick to the database.
 */

using System;
using System.ComponentModel;

namespace i4pro_gruppe_d.Types
{
    public class Brick : INotifyPropertyChanged
    {
        public Brick(DateTime time = new DateTime(), int height = 0, int width = 0, int length = 0, int weight = 0)
        {
            Time = time;
            Height = height;
            Width = width;
            Length = length;
            Weight = weight;
            CalcType();
        }
        private int brickType;
        public int BrickType
        {
            get { return brickType; }
            set
            {
                brickType = value;
                InvokePropertyChanged("BrickType");
            }
        }
        public void CalcType()
        {
            double volume = Height*Width*Length;
            if (volume > 0 && Weight > 0)
            {
                double density = Weight / (volume / 1000);
                                
                Density = density;
                if (density < 0.84)
                    BrickType = 1;
                else if (density < 0.97)
                    BrickType = 2;
                else if (density < 1.3)
                    BrickType = 3;
                else if (density < 1.6)
                    BrickType = 4;
                else if (density < 1.9)
                    BrickType = 5;
            }
            else
                BrickType = -1;
        }

        private double _density;
        public double Density
        {
            get { return _density; }
            set
            {
                string dens = value.ToString("N2");
                _density = double.Parse(dens);
                InvokePropertyChanged("Density");
            }
        }

        private DateTime _time;
        public DateTime Time
        {
            get { return _time; }
            set
            {
                _time = value;
                InvokePropertyChanged("Time");
            }
        }

        private int _height;
        public int Height
        {
            get { return _height; } 
            set
            {
                if (value <= 70 && value > 0)
                {
                    _height = value;
                    InvokePropertyChanged("Height");
                    CalcType();
                }
            }
        }

        private int _width;
        public int Width
        {
            get { return _width; }
            set
            {
                if (value <= 70 && value > 0)
                {
                    _width = value;
                    InvokePropertyChanged("Width");
                    CalcType();
                }
            }
        }

        private int _length;
        public int Length
        {
            get { return _length; }
            set
            {
                if (value <= 70 && value > 0)
                {
                    _length = value;
                    InvokePropertyChanged("Length");
                    CalcType();
                }
            }
        }

        private int _weight;
        public int Weight
        {
            get { return _weight; }
            set
            {
                if (value <= 950 && value > 0)
                {
                    _weight = value;
                    InvokePropertyChanged("Weight");
                    CalcType();
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void InvokePropertyChanged(String propertyName)
        {
            PropertyChangedEventArgs e = new PropertyChangedEventArgs(propertyName);

            PropertyChangedEventHandler changed = PropertyChanged;

            if (changed != null) changed(this, e);
        }
    }
}
